<template>
  <div>
    <base-right-dialog
      ref="baseRightDialog"
      :footerShow="true"
      :dialogVisible.sync="dialogVisible"
      :title="dialogTitle + ' 集成应用配置'"
      @handleClose="handleDialogClose"
      :type="dialogType"
      :submitShow="submitShow"
      @handleConfirmClick="handleConfirmClick"
    >
      <div class="dialogList">
        <base-form
          ref="basicsForm"
          :formRow="formRow"
          :isFunBtn="false"
          :rules="basicsRules"
          class="dialog_form"
          :spanWidth="`120px`"
          :loading="vLoading"
          @onSelect="onSelect"
        ></base-form>
      </div>
    </base-right-dialog>
  </div>
</template>

<script>
import baseRightDialog from "@/components/base/baseRightDialog";
import baseForm from "@/components/base/baseNewForm";
import configData from "./configData";
import { authApi } from "@/api/apis/auth";
export default {
  components: {
    baseRightDialog,
    baseForm,
  },
  data() {
    return {
      dialogVisible: false,
      dialogTitle: "",
      dialogType: "",
      formRow: configData.formRow,
      basicsRules: configData.basicsRules,
      vLoading: false,
      newMarryOptions: [],
      submitShow: true,
      loadingType: true,
    };
  },
  mounted() {},
  methods: {
    openDialog(type, row) {
      this.queryProductAll();
      this.selectDictionaryshop();
      this.formRow = configData.formRow;
      // 新增
      if (type == "add") {
        this.submitShow = true;
        this.dialogTitle = "新增";
        this.dialogType = "add";
      }
      // 编辑
      if (type == "edit") {
        this.submitShow = true;
        this.dialogTitle = "编辑";
        this.dialogType = "edit";
        this.applicationGetById(row.id);
      }
      // 查看
      if (type == "show") {
        this.submitShow = false;
        this.formRow = configData.formRowShow;
        this.dialogTitle = "查看";
        this.dialogType = "show";
        this.applicationGetById(row.id);
      }
      this.dialogVisible = true;
    },
    // 编辑详情
    async applicationGetById(id) {
      let params = {
        id: id,
      };
      let res = await authApi("sysAppService", "app", "queryApp", "", params);
      if (res.status == "200") {
        this.$nextTick(() => {
          if (res.attribute.registeredProductCode) {
            this.queryProductVersion(res.attribute.registeredProductCode);
          }
          this.$refs.basicsForm.incomingParameters(res.attribute);
        });
      }
    },
    // 弹窗关闭
    handleDialogClose() {
      this.$refs.basicsForm.resetFields();
      this.dialogVisible = false;
    },
    // 弹窗确认按钮
    handleConfirmClick() {
      this.$refs.basicsForm.$refs["ruleForm"].validate((valid) => {
        if (!valid) {
          return;
        } else {
          let params = {
            ...this.$refs.basicsForm.ruleForm,
          };
          if (this.dialogType == "add") {
            this.openLoading("submit");
            this.applicationSaveDto(params);
          }
          if (this.dialogType == "edit") {
            this.openLoading("submit");
            this.applicationUpdateDto(params);
          }
        }
      });
    },
    // 新增保存
    async applicationSaveDto(params) {
      let res = await authApi("sysAppService", "app", "addApp", "", params);
      if (res.status == "200") {
        this.handleDialogClose();
        this.$vmNews("新增成功", "success");
        this.$emit("resetTable");
      }
    },
    // 编辑保存
    async applicationUpdateDto(params) {
      let res = await authApi("sysAppService", "app", "updateApp", "", params);
      if (res.status == "200") {
        this.handleDialogClose();
        this.$vmNews("更新成功", "success");
        this.$emit("resetTable");
      }
    },
    // 获取所有登记产品
    async queryProductAll() {
      let params = {};
      let res = await authApi(
        "sysProductService",
        "product",
        "queryProductAll",
        "",
        params
      );
      if (res.status == "200") {
        this.formRow[1].elCol[0].options = res.attribute;
      }
    },
    onSelect(val, index, indexRow, obj) {
      if (index == 1 && val) {
        this.queryProductVersion(val);
      }
    },
    // 根据产品id获取该产品所有的版本
    async queryProductVersion(id) {
      let params = {
        productId: id,
      };
      let res = await authApi(
        "sysProductVersionService",
        "product",
        "queryProductVersion",
        "",
        params
      );
      if (res.status == "200") {
        this.formRow[2].elCol[0].options = res.attribute;
      }
    },
    // 接入方式
    async selectDictionaryshop() {
      let params = {
        tab_name: "sys_app",
        column_name: "access_mode",
      };
      let res = await authApi(
        "generalServiceImpl",
        "dictionaryshop",
        "selectDictionaryshop",
        "",
        params
      );
      if (res.status == "200") {
        this.formRow[7].elCol[0].options = res.attribute;
      }
    },
  },
};
</script>

<style>
</style>